package com.smile.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smile.api.FlyApi;
import com.smile.commons.page.MyBatisPlusPageConverter;
import com.smile.commons.page.R;
import com.smile.model.dto.request.*;
import com.smile.model.request.*;
import com.smile.model.response.Drone;
import com.smile.model.response.GetAirportByIdResponse;
import com.smile.model.response.GetAirportListResponse;
import com.smile.model.response.response.GetAirportByIdResponseDTO;
import com.smile.model.response.response.GetAirportListResponseDTO;
import com.smile.service.AirportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/airport")
public class AirportController {

    @Autowired
    private AirportService airportService;

    @Value("${spring.redis.host}")
    private String redisHost;


    @PostMapping("/getAirportDetailInfo")
    public R getAirportDetailInfo(@RequestBody GetAirportDetailInfoRequest getAirportDetailInfoRequest) {
        return R.ok();
    }

//    @SaIgnore
    @PostMapping("/getAirportById")
    public R<GetAirportByIdResponse> getAirportById(@RequestBody GetAirportByIdRequest getAirportByIdRequest) {
        System.out.println(redisHost);
        GetAirportByIdRequestDTO getAirportByIdRequestDTO = BeanUtil.copyProperties(getAirportByIdRequest, GetAirportByIdRequestDTO.class);
        GetAirportByIdResponseDTO getAirportByIdResponseDTO = airportService.getAirportById(getAirportByIdRequestDTO);
        GetAirportByIdResponse getAirportByIdResponse = BeanUtil.copyProperties(getAirportByIdResponseDTO, GetAirportByIdResponse.class);
        getAirportByIdResponse.setDroneList(BeanUtil.copyToList(getAirportByIdResponseDTO.getDroneDTOs(), Drone.class));
        getAirportByIdResponse.setWindDirection("东南风");
        return R.ok(getAirportByIdResponse);
    }

    @PostMapping(value = "/editAirport")
    public R<Void> editAirport(@Valid @RequestBody EditAirportRequest editAirportRequest, HttpServletRequest request) throws IOException {
        EditAirportRequestDTO editAirportRequestDTO = new EditAirportRequestDTO();
        BeanUtil.copyProperties(editAirportRequest, editAirportRequestDTO);
        Boolean result = airportService.editAirport(editAirportRequestDTO);
        if (result) {
            return R.ok();
        } else {

            return R.fail();
        }
    }

    @PostMapping("/deleteAirport")
    public R<Void> deleteAirport(@RequestBody Long id) {
//        DeleteAirportRequestDTO deleteAirportRequestDTO = BeanUtil.copyProperties(deleteAirportRequest, DeleteAirportRequestDTO.class);
        Boolean result = airportService.deleteAirport(id);
        if (result) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @PostMapping("/getAirportList")
    public R<Page<GetAirportListResponse>> getAirportList(@RequestBody GetAirportListRequest getAirportListRequest) {
        GetAirportListRequestDTO getAirportListRequestDTO = new GetAirportListRequestDTO();

        BeanUtil.copyProperties(getAirportListRequest, getAirportListRequestDTO);
        Page<GetAirportListResponseDTO> pages = airportService.getAirportList(getAirportListRequestDTO);

        return R.ok(MyBatisPlusPageConverter.convertPage(pages, GetAirportListResponse.class));
    }

    @PostMapping("/getAllAirportList")
    public R<List<GetAirportListResponse>> getAllAirportList(@RequestBody GetAirportListRequest getAirportListRequest) {
        GetAirportListRequestDTO getAirportListRequestDTO = new GetAirportListRequestDTO();

        BeanUtil.copyProperties(getAirportListRequest, getAirportListRequestDTO);
        List<GetAirportListResponseDTO> pages = airportService.getAllAirportList(getAirportListRequestDTO);
        List<GetAirportListResponse> list = new ArrayList<>();
        for (GetAirportListResponseDTO feisuanAirport : pages) {
//            System.out.println(feisuanAirport);
            GetAirportListResponse getAirportListResponseDTO = BeanUtil.copyProperties(feisuanAirport, GetAirportListResponse.class);
            System.out.println(getAirportListResponseDTO);
            list.add(getAirportListResponseDTO);
        }
        return R.ok(list);
    }
}
